package cn.dapeng.math;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.math
 * @Description TODO
 * @ClassName MathUtil
 * @Author xupenghong
 * @Date 2024/9/26 8:47
 * @Version 1.0.0
 */
public class MathUtil {


    /**
     * greatest common divisor
     *
     * @return 求两个数字的最大公约数 递归方式
     */
    public static long gcd_recursion(long a, long b) {
        return b == 0 ? a : gcd_recursion(b, a % b);
    }

    /**
     * greatest common divisor
     *
     * @return 求两个数字的最大公约数 非递归方式
     */
    public static long gcd(long a, long b) {
        long max = Math.max(a, b);
        long min = max == a ? b : a;

        if (min == 0) {
            return max;
        }
        long temp;
        while ((temp = max % min) != 0) {
            max = min;
            min = temp;
        }
        return min;
    }

    /**
     * Least common multiple
     *
     * @param a
     * @param b
     * @return 求两个数字的最小公倍数
     */
    public static long lcm(long a, long b) {
        return a * b / gcd_recursion(a, b);
    }

    public static void main(String[] args) {

        int testTimes = 100000;
        for (int i = 0; i < testTimes; i++) {
            long num = (long) (Math.random() * 10000);

            if (gcd_recursion(num, 20) != gcd(num, 20)) {
                System.out.println(num + " Oops");
                break;
            }
        }
    }

}
